package com.内部类.匿名内部类;

public class AnonymousInnerClass02 {
    /**基于类的匿名内部类*/
    public static void main(String[] args) {
        new Outer01().method();
    }
}

class Outer01{
    public void method(){
        Father father01 = new Father("成龙");  //创建Father对象
        System.out.println("father的运行类型是"+father01.getClass());  //输出father01的运行类型Father

        Father father02 = new Father("周杰伦"){ //定义并创建匿名内部类
/**      Father后面加了{}是代表  定义并创建匿名内部类，继承父类Father
 *      class Outer$1 extends Father{ }
 *      然后再创建赋给父类引用father02  Father father02 = new Outer$1( ) 然后把"周杰伦"传入到父类的构造器来继承 */
            public void test(){
                System.out.println("匿名内部类的test方法重写Father类的方法");
            }
        };
        System.out.println("father02的运行类型是"+father02.getClass()); //输出father02的运行类型是Outer$1
        father02.test();  //继承并重写了父类Father的test方法，动态绑定，调用Outer$1test方法
    }

}

/***   Father father02 = new Father("周杰伦"){}；
 * 相当于定义了一个类继承父类Father，然后创建了该类的对象，并把对象赋值给父类引用father02
 * class Outer$1 extends Father{
      *public void test(){
     *                 System.out.println("匿名内部类的test方法重写Father类的方法");
     *             }
 * }
 * Father father02 = new Outer$1() 然后把"周杰伦"传入到父类的构造器来继承 ;
 */




class Father{
     private String name;

    public Father(String name) {
        this.name = name;
    }
    public void test(){
        System.out.println("Father.test");
    }
}